package com.lengxf.leetcode.a;

import java.util.Arrays;

public class Leetcode_insertionSort {

    public static void main(String[] args) {
        int[] arr = GenerateArr.getArr(10);
        System.out.println("开始前 : " + Arrays.toString(arr));
        insertSort(arr);
        System.out.println("结束后 : " + Arrays.toString(arr));
    }

    /**
     * 插入排序
     **/
    private static void insertSort(int[] arr) {
        if (arr == null || arr.length <= 1) {
            return;
        }
        int current, preIndex;
        for (int i = 1; i < arr.length; i++) {
            current = arr[i];
            preIndex = i - 1;
            while (preIndex >= 0 && current < arr[preIndex]) {
                arr[preIndex + 1] = arr[preIndex--];
            }
            arr[preIndex + 1] = current;
        }
    }


}

